package demo;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:恋恋
 * Date:2022-09-14
 * Time:15:17
 */
class Animal{
    String name;
    int age;
    public Animal(String name,int age){
        this.name=name;
        this.age=age;
    }
    public void eat(){
        System.out.println(name+"吃饭");
    }
    public void function(){
        eat();
    }
}
class Dog extends Animal{
    //如果父类的构造方法用户自己编写，且带有参数---》因此子类必须重写构造方法
    public Dog(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println(name+"吃狗粮");
    }
    public void sleep(){
        System.out.println(name+"睡狗窝");
    }
}
class Cat extends Animal{
    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println(name+"吃猫料");
    }
    public void sleep(){
        System.out.println(name+"睡猫窝");
    }
}
public class Test {
    //3、作方法返回值：返回任意子类对象
    public static Animal eatFood(String Var){
        if("狗".equals(Var)){
            return new Dog("柯基3",6);
        }else if("猫".equals(Var)){
            return new Cat("豹纹3",1);
        }else{
            return null;
        }
    }
    public static void main3(String[] args) {
        eatFood("狗").eat();
        eatFood("猫").eat();
    }
   //2、方法传参：参数为父类类型引用，可以接收任意子类的对象
    public static void eatFood(Animal animal){

        animal.eat();
    }

    public static void main4(String[] args) {
        eatFood(new Dog("柯基2",6));
        eatFood(new Cat("豹纹2",1));
    }
    public static void main(String[] args) {
        //1、直接赋值：子类对象赋值给父类对象
        Animal animal1=new Dog("柯基",6);
        Animal animal2=new Cat("豹纹",1);
//        animal1.eat();
//        animal2.eat();
//        Cat cat=(Cat) animal1;
//        cat.sleep();
        if(animal1 instanceof Dog){
            Dog dog=(Dog) animal1;
            dog.sleep();
        }



    }
}
